package cn.com.cifi.mars.common.base;

import cn.com.cifi.mars.common.bean.R;
import cn.com.cifi.mars.common.redis.RedisTemplateUtil;
import cn.com.cifi.mars.entity.common.IdmUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 
 * 类名称：BaseAction
 * 类描述：基础类
 * @author ：yyd
 * 创建时间：2019年12月19日 上午11:51:10
 * @version 1.0.0
 */
@Controller
@Slf4j
public class BaseAction {

    @Autowired
    private RedisTemplateUtil redisTemplateUtil;

    public R fail(String msg) {
        return new R(R.CODE_FAIL, msg);
    }

    public R fail(String msg, Object object) {
        return new R(R.CODE_FAIL, msg, object);
    }

    public R ok(Object obj) {
        return new R(obj);
    }

    public R ok() {
        return ok("ok");
    }

    public final IdmUser getOperator() {

        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

        String sessionToken = request.getHeader("sessionToken");
        if (sessionToken == null) {
        	sessionToken = request.getParameter("sessionToken");
        }

        final IdmUser user = (IdmUser) redisTemplateUtil.get("mars_user_" + sessionToken);

        return user;
    }

}